﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core;
using Cofe.Core.Filter;
using Cofe.Core.Implements;
using Cofe.Data.Utils.ThirdParty;

namespace Cofe.Data.Filter
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFEDB)]
    public class OrEntryFilterExpression : 
        IEntryFilterExpression<OrEntryFilter>
    {
        public Expression<Func<CofeSystemInfo, bool>>
            GetQueryExpression(OrEntryFilter entryFilter, IOptionFilter[] options)
        {
            Expression<Func<CofeSystemInfo, bool>> exp = null;
            foreach (var ef in entryFilter.EntryFilters)
            {
                var currentExpr = CofeServices.ServiceLocater.FindService<IEntryFilterExpressionFactory>()
                .ConstructExpression(FilterCollections.FromFilters(new IEntryFilter[] { ef }, options));
                if (exp == null)
                    exp = currentExpr;
                else exp = LambaUtils.Or(exp, currentExpr);

            }            
      
            if (entryFilter.Negative)
                return exp.Not();
            return exp;
        }

    }
}
